<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class GoodCommentController extends Controller
{

    /**
     * 静态方法进行权限判断
     * 
     * @param         string       $operation_name        判断角色管理是否关闭
     * @param         string       $role_func             判断管理员权限是否开启*/
    static public function func($operation_name,$role_func){
        // 进行session判断操作管理是否关闭,管理员权限是否开启，如果违法操作则返回
        if(session($operation_name) == 2 || empty(strstr(session('role_func'),$role_func))){
                return true;
        }
    }


    /**
     * 商品评论
     *
     * @param       int        $count        获取分页需求
     * @param       string     $search       获取搜索需求
     * @param       int        $goods_id     模拟商品id ---------------后期需修改
     * @param       array      $data         获取所有商品评论信息
     * @return      返回评论列表模板
     **/
   public function getIndex(Request $request)
   {
    
        $count = $request -> input('count','10');
        $search = $request -> input('search','');
        $grade = [$request -> input('grade',0)];
        if($grade == [0]){
            $grade = [1,2,3];
        }
        $goods_id = 1;
        $data = DB::table('goods_comment') 
        -> join('users','goods_comment.user_id','=','users.id')
        -> where('goods_id',$goods_id) 
        -> where('user_email','like','%'.$search.'%')
        -> whereIn('goods_comment_grade',$grade)
        -> paginate($count);
        return view('/admin/goodsComment/list',['title'=>'商品评论列表','data'=>$data]);
   }

   /**
    * 获取商品评论内容：商品管理员权限
    *
    * @param       int           $comment_id            评论id
    * @param       array         $data                  评论表与用户表内容
    * @param       string        $$goods_comment_photo  图片字符串信息
    * @return      返回详情模板
    */
   public function getDetails($comment_id)
   {

        $data = DB::table('goods_comment') 
        -> join('users','users.id','=','goods_comment.user_id')
        -> where('comment_id',$comment_id)
        -> first();
        

        // 获取图片字符串
        $goods_comment_photo = $data['goods_comment_photo'];
        // 将图片字符串转为数组
        $arr = explode(',',$data['goods_comment_photo']);
        // 赋予data 图片数组
        $data['goods_comment_photo'] = $arr;
        return view('/admin/goodsComment/details',['title'=>'评论详情','data'=>$data]);
   }

    /**
     * 商品回复评论
     * 
     * @param       array       $arr         获取商家回复内容
     * @param       int         $comment_id  获取评论id
     * @param       bool        $res         返回true or false
     * @return      返回评论模板
     */
   public function postReply(Request $request)
   {
        $arr = $request->except(['_token']);
        $comment_id = $request -> input('comment_id');
        $res = DB::table('goods_comment') ->where('comment_id',$comment_id) -> update($arr);
        if($res){
            return back() -> with('success','回复评论成功');
        }else{
            return back() -> with('error','不可以重复回复');
        }
   }


   public function getDelete(Request $request)
   {
        $res = DB::table('goods_comment') -> where('comment_id',$request->input('id')) -> delete();
        if($res){
            return back() -> with('success','删除评论成功');
        }else{
            return back() -> with('error','删除失败');
        }
   }
}
